1
Streams as the Foundation of Ruby I/O
AI035 Lesson 6
00:00

In Ruby, Input/Output isn't just a set of commands; it is a sophisticated stream-based architecture. At the heart of this system lies the IO class, which serves as a bidirectional channel between your program and the external world. Whether you are interacting with a file, a network socket, or the user's terminal, Ruby treats them as generic streams.

1. The Bidirectional Bridge

An IO object is a unified abstraction. While the operating system sees distinct file descriptors for reading and writing (like in a pipe), Ruby encapsulates these into a single object. This allows for seamless data flow in both directions.

Ruby ProgramResourceIO Object

2. Kernel & Standard Filters

The Kernel module provides methods like gets and print. These are essentially filters that delegate to the global constants STDIN and STDOUT. This means your code can be redirected at the OS level to process files or network streams without changing a single line of logic.

$$\text{Stream} \rightarrow \text{Buffer} \rightarrow \text{Ruby Interpreter}$$

main.py
TERMINAL bash — 80x24
> Ready. Click "Run" to execute.
>